﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：MenusContextMenu.cs
           // 文件功能描述：具有添加菜单的右键菜单。
           //
           // 
           // 创建标识：杨波20080903
           //
           // 修改标识：
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.Data;

#endregion

namespace RuleCode.ObjectTree
{
    public partial class MenusContextMenu : SychronizeContextMenu
    {
        protected string rootMenuName;

        public MenusContextMenu()
        {
            InitializeComponent();
        }

        private void addMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string name = Util.addNode(OT.XPathSeperater + OT.Menus, OT.Menu, typeof(MenuProperty), "");
            ((NameProperty)Util.SelectedNode.Property).Name = name;
        }

        protected override void sycToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, "Delete  from Menu ");
            Util.SelectedNode.ExpandAllNodes();
            foreach (SysTreeNode node in Util.SelectedNode.Nodes)
            {
                rootMenuName = node.Text;
                foreach (SysTreeNode subNode in node.Nodes)
                {
                    addSubNode(subNode, node);
                }
                /*INSERT INTO  [dbo].[Menu]
           ([DATAAREAID]
           ,[RECID]
           ,[MenuName]
           ,[MenuID]
           ,[MenuType]
           ,[MenuLabel]
           ,[MenuParentID])
     VALUES
           (<DATAAREAID, varchar(3),>
           ,<RECID, bigint,>
           ,<MenuName, varchar(20),>
           ,<MenuID, varchar(20),>
           ,<MenuType, varchar(20),>
           ,<MenuLabel, varchar(20),>
           ,<MenuParentID, varchar(20),>)*/
            }
            base.sycToolStripMenuItem_Click(sender, e);
        }

        private void addSubNode(SysTreeNode node, SysTreeNode parentNode)
        {
            Util.SetPropertyContent(node);
            Util.SetPropertyContent(parentNode);
            if (node.Property is MenuItemInMenuProperty)
            {
                MenuItemInMenuProperty property = (MenuItemInMenuProperty)node.Property;
                //SqlHelper.Instance.ExecuteNonQuery(CommandType.Text,
                //                                   "INSERT INTO  [dbo].[目录]([DATAAREAID],[RECID],[名称],[目录编码],[目录类型],[标签编码],[目录项编码],[父目录编码])VALUES('" +
                //                                   "" + "',"
                //                                   + Convert.ToString(Util.GetNewRecId)
                //                                   + ",'" + rootMenuName + "','" + node.Property.ID + "', " + 1 + " ,'" +
                //                                   property.Label + "','" + property.MenuItemName + "','" +
                //                                   parentNode.Property.ID + "')");
                SqlHelper.Instance.ExecuteNonQuery(CommandType.Text,
                                                 "INSERT INTO  [dbo].[Menu]([DATAAREAID],[RECID],[Name],[MenuID],[MenuType],[Label],[MenuItemID],[PMenuID])VALUES('" +
                                                 "" + "',"
                                                 + Convert.ToString(Util.GetNewRecId)
                                                 + ",'" + rootMenuName + "','" + node.Property.ID + "', " + 1 + " ,'" +
                                                 property.Label + "','" + property.MenuItemName + "','" +
                                                 parentNode.Property.ID + "')");
            }
            else
            {
                MenuProperty property = (MenuProperty)node.Property;
                //SqlHelper.Instance.ExecuteNonQuery(CommandType.Text,
                //                                   "INSERT INTO  [dbo].[目录]([DATAAREAID],[RECID],[名称],[目录编码],[目录类型],[标签编码]" +
                //                                   ",[父目录编码])VALUES('" + "" + "',"
                //                                   + Convert.ToString(Util.GetNewRecId)
                //                                   + ",'" + rootMenuName + "','" + node.Property.ID + "', " + 0 + " ,'" +
                //                                   property.Label + "','" + parentNode.Property.ID + "')");
                SqlHelper.Instance.ExecuteNonQuery(CommandType.Text,
                                                 "INSERT INTO  [dbo].[Menu]([DATAAREAID],[RECID],[Name],[MenuID],[MenuType],[Label]" +
                                                 ",[PMenuID])VALUES('" + "" + "',"
                                                 + Convert.ToString(Util.GetNewRecId)
                                                 + ",'" + rootMenuName + "','" + node.Property.ID + "', " + 0 + " ,'" +
                                                 property.Label + "','" + parentNode.Property.ID + "')");
            }

            node.ExpandAll();
            if (node.GetNodeCount(false) > 0)
            {
                foreach (SysTreeNode subnode in node.Nodes)
                {
                    addSubNode(subnode, node);
                }
            }
        }
    }
}